package it.cqupt.dao;

import it.cqupt.bean.Student;
import it.cqupt.mapper.StudentMaper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class StudentTest1 {
    @Test
    public void selectAll()throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();
        List<Student> stus = sqlSession.selectList("StudentMapper.selectAll");
        for (Student student : stus) {
            System.out.println(student);
        }
        sqlSession.close();
        is.close();
    }
    @Test
    public void selectById()throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();
        Student stu = sqlSession.selectOne("StudentMapper.selectById", 2);
        System.out.println(stu);
    }
    @Test
    public void insert()throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        Student stu = new Student(5,"小锤",10);
        int num = sqlSession.insert("StudentMapper.insertToStu", stu);
//        sqlSession.commit();
        System.out.println(num);
        sqlSession.close();
        is.close();
    }
    @Test
    public void updateNameById()throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();

        int num = sqlSession.update("StudentMapper.updateNameById", 1);
        sqlSession.commit();
        System.out.println(num);
    }
    @Test
    public void updateAllById() throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();
        Student stu = new Student(1,"马玉",15);
        int update = sqlSession.update("StudentMapper.updateAllById",stu);
        sqlSession.commit();
    }
    @Test
    public void delete()throws Exception{
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession();
        int num = sqlSession.delete("StudentMapper.deleteById", 5);
        sqlSession.commit();
        System.out.println(num);
    }
    @Test
    public void selectByCondition(){
        InputStream is = null;
        SqlSession sqlSession =  null;
        Student stu = new Student();
        stu.setSid(2);
//        stu.setName("李四");
//        stu.setAge(20);
        try{
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMaper stuMap = sqlSession.getMapper(StudentMaper.class);
            List<Student> students = stuMap.selectByCondition(stu);
            for(Student student : students){
                System.out.println(student);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void selectStudentByIds()throws Exception{
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = build.openSession(true);
        StudentMaper stuMapper = sqlSession.getMapper(StudentMaper.class);
        List<Student> students = stuMapper.selectStudentByIds(list);
        for(Student student : students){
            System.out.println(student);
        }
    }
}
